//
//  DetailBarnerView.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/1.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "DetailBarnerView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ACMacros.h"

@implementation DetailBarnerView {
    NSInteger pageCount;
    CGFloat bannerWidth;
    CGFloat bannerHeight;
    
    NSArray *imagesArray;
    NSArray *imageDataUrls;
    
    NSTimer *timer;
    CGFloat currentPage;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    self.mainScrollView.delegate = self;
    [self.room_A_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.room_B_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.room_C_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.room_A_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
    [self.room_B_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
    [self.room_C_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
    
}

- (void)refreshBannerWithImages {
    for (id item in self.mainScrollView.subviews) {
        if ([item class] == [UIImageView class] || [item class] == [UIWebView class]) {
            [item removeFromSuperview];
        }
    }
    NSInteger count = imagesArray.count;
    [self.mainScrollView setContentSize:CGSizeMake(count * Main_Screen_Width, 0)];
    [self.pageLabel setText:[NSString stringWithFormat:@"%d/%ld",1,(long)count]];
    if (count == 0) {
        [self.mainScrollView setContentOffset:CGPointMake(0, 0)];
        //TestLog
        NSLog(@"banner dataArray is Nil");
    } else {
        
        for (int i = 0; i < count; i++) {
            UIImageView *bannerPageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * Main_Screen_Width, 0, Main_Screen_Width, 307)];
            [self.mainScrollView addSubview:bannerPageView];
            [bannerPageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",imagesArray[i]]]];
            bannerPageView.contentMode = UIViewContentModeScaleToFill;
            currentPage = 0;
//            [self startAutoPaging];
        }
    }
}

- (void)refreshBannerWithImageUrls:(NSArray *)urls {
    imagesArray = urls;
    [self refreshBannerWithImages];
}

- (IBAction)room_A_ButtonClicked:(UIButton *)sender {
    [sender setSelected:!sender.isSelected];
    if (sender.isSelected) {
        [self.room_B_Button setSelected:false];
        [self.room_C_Button setSelected:false];
        [self.room_A_Button.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
        [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    } else {
        [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    }
    [self.delegate buttonClicked:sender];
}
- (IBAction)room_B_ButtonClicked:(UIButton *)sender {
    [sender setSelected:!sender.isSelected];
    if (sender.isSelected) {
        [self.room_A_Button setSelected:false];
        [self.room_C_Button setSelected:false];
        [self.room_B_Button.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
        [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    } else {
        [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    }
    [self.delegate buttonClicked:sender];
}

- (IBAction)room_C_ButtonClicked:(UIButton *)sender {
    [sender setSelected:!sender.isSelected];
    if (sender.isSelected) {
        [self.room_A_Button setSelected:false];
        [self.room_B_Button setSelected:false];
        [self.room_C_Button.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
        [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    } else {
        [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    }
    [self.delegate buttonClicked:sender];
}
- (IBAction)hallButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)kitchenButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)tolietButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)balconyButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)communityButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)moreDetailClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)LikeButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat width = scrollView.contentSize.width;
    int curx = ceil(offsetX);
    int tempBannerWidth = ceil(Main_Screen_Width);
    int page = ceil(curx/tempBannerWidth + 1);
    int totalPage = ceil(width/tempBannerWidth);
    [self.pageLabel setText:[NSString stringWithFormat:@"%d/%d",page,totalPage]];
}

@end
